#include "client.h"

/* Usage:./client ip port */
int main(int argc, char *argv[]){
    ARGS_CHECK(argc, 3);

    // 和客户端建立TCP链接
    int sockfd = tcpInit(argv[1], argv[2]);

    // 存储用户名、密码、token
    char username[4096] = {0};
    char passwd[4096] = {0};
    char token[41] = {0};

    // 判断token
    int tokenExist = (strlen(token) == 0) ? -1 : 1;
    send(sockfd, &tokenExist, sizeof(tokenExist), MSG_NOSIGNAL);
    if(tokenExist == -1){

        // 登陆验证
        loginCheck(username, sockfd, passwd);

        // 接收token
        int ret = recv(sockfd, token, sizeof(token), MSG_WAITALL);
        if(ret == 0){
            printf("服务端已退出！\n");
            exit(-1);
        }
    }

    // 命令识别并处理
    while(1){
        dealCommand(sockfd, username, passwd, token, argv[1], argv[2]);
    }

    close(sockfd);

    return 0;
}

